package com.cheer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.dao.MallImageDao;
import com.cheer.entity.MallImageEntity;
import com.cheer.enums.MallImageEnum;
import com.cheer.service.MallImageService;
import com.cheer.vo.MallImageLinkVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("mallImageService")
public class MallImageServiceImpl extends ServiceImpl<MallImageDao, MallImageEntity> implements MallImageService {

    @Autowired
    private MallImageDao mallImageDao;

    @Value("${image.url}")
    private String imageUrl;

    @Override
    public List<MallImageLinkVO> listTopImages() {
        List<MallImageEntity> imageList = mallImageDao.listByType(MallImageEnum.TOP.getCode());

        return imageList.stream()
                .map(imageEntity -> new MallImageLinkVO()
                        .setProductId(imageEntity.getProductId())
                        .setUrl(imageUrl + imageEntity.getImageUrl())
                ).collect(Collectors.toList());
    }

    @Override
    public String getWatermarkImage() {
        List<MallImageEntity> imageList = mallImageDao.listByType(MallImageEnum.WATERMARK.getCode());
        if (CollectionUtils.isNotEmpty(imageList)) {
            return imageUrl + imageList.get(0).getImageUrl();
        }
        return null;
    }
}